<?php 
class File extends Object
{
	public $Type;
	public $Title;
	public $Description;
	public $Filename;
	public $FileOrder;
	public $Active;
	public $ParentIdField;
	public $ParentId;
	public $FolderPath;
	public $FolderUrl;
	public $Icon;
	public $IconUrl;
	
	public function File($values = '')
	{ 
		if(is_array($values))
		{ self::setValues($values); }
	}
	
	public function getFileOrder() { return intval($this->FileOrder); }
	public function getParentId() { return intval($this->ParentId); }
	
	public function setFileOrder($val) { $this->FileOrder=intval($val); }
	public function setParentId($val) { $this->ParentId=intval($val); }
	
	public function getLink()
	{
		$link = self::getFolderUrl().self::getFilename();
		return $link;
	}
	
	public function getIcon($size=24)
	{
		$ext = self::extractExtension(self::getFilename());
		$out = '<img src="'.SYSICONS_URL.'/mimetypes/'.$size.'/';
		
		if(is_file(SYSICONS_URL.'/mimetypes/'.$size.'/'.$ext.'.png'))
		{ $out .= $ext.'" />'; }
		else
		{
			switch($ext)
			{
				case 'pdf':
					$out .= 'pdf.png" />';
				break;
				default:
					$out .= 'txt.png" />';
				break;
			}
		}
		return $out;
	}
	
	function GetFileFromUrl($link)
	{ return file_get_contents($link); }

	public function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()) && (self::getDataTable() != ''))
		{
			$sql  = 'UPDATE '.DB_PERFIX.self::getDataTable().' SET ';
			if(self::getType() != '')
			{ $sql .= '`Type`="'.self::formatText(self::getType()).'", '; }
			if(self::getTitle() != '')
			{ $sql .= '`Title`="'.self::formatText(self::getTitle()).'", '; }
			$sql .= '`Filename`="'.self::formatText(self::getFilename()).'", ';
			if((self::getParentIdField() != '') && (self::getParentId() > 0))
			{ $sql .= '`FileOrder`='.self::getFileOrder().', '; }
			$sql .= '`active`='.self::getActive().' ';
			$sql .= ' WHERE Id='.self::getId().' ';
			if((self::getParentIdField() != '') && (self::getParentId() > 0))
			{ $sql .= ' AND `'.self::getParentIdField().'`='.self::getParentId().' '; }
			$sql .= ' LIMIT 1;';
			mysql_query($sql);
		}
	}
	
	public function extractExtension($file)
	{
		$ext = '';
		if(is_array($file))
		{ $fname = $file['name']; }
		else
		{ $fname = $file; }
		if($fname != '')
		{
			$fnameParts = array_reverse(explode(".",$fname));
			$ext = str_ireplace("jpeg","jpg",strtolower($fnameParts[0]));
		}
		return $ext;
	}
	
	public function validateFile($file)
	{
		$valid = 0;
		if(is_array($file))
		{
			if(($file['name'] != '') && ($file['size'] > 0) && (self::extractExtension($file) != ''))
			{
				switch(self::extractExtension($file))
				{
					case 'jpg':
					case 'png':
					case 'pdf':
					case 'doc':
					case 'xls':
						$valid = 1;
					break;
				}
			}
		}
		return $valid;
	}
	
	
	public function getValidExt()
	{
		$extList['gif'] 	= 'image/gif';
		$extList['jpg'] 	= 'image/jpeg';
		$extList['jpeg'] 	= 'image/jpeg';
		$extList['png'] 	= 'image/png';
		$extList['pdf'] 	= 'text/pdf';
		return $extList;
	}
	
	public function setValidExt($type)
	{
		$fileTypes  = array(  
		'image/pjpeg'     => 'jpg',
		'image/jpeg'     => 'jpg',
		'image/gif'     => 'gif',
		'image/X-PNG'    => 'png',
		'image/PNG'         => 'png',
		'image/png'     => 'png',
		'image/x-png'     => 'png',
		'image/JPG'     => 'jpg',
		'image/GIF'     => 'gif',
		'application/unknown' => 'pdf',
		);
		
		if($fileTypes[$type] != '')
		{ return $fileTypes[$type]; }
	}
	
	public function fileValid($type)
	{
		
		$file_types  = array(  
		'image/pjpeg'     => 'jpg',
		'image/jpeg'     => 'jpg',
		'image/jpeg'     => 'jpeg',
		'image/gif'     => 'gif',
		'image/X-PNG'    => 'png',
		'image/PNG'         => 'png',
		'image/png'     => 'png',
		'image/x-png'     => 'png',
		'image/JPG'     => 'jpg',
		'image/GIF'     => 'gif',
		'application/unknown' => 'pdf',
		);
	   
		if(!array_key_exists($type, $file_types))
		{ return false; }
		else
		{ return true; }
	}
	
	public function reArrayFiles(&$file_post) 
	{
		$file_ary = array();
		$file_count = count($file_post['name']);
		$file_keys = array_keys($file_post);
		for ($i=0; $i<$file_count; $i++) 
		{
			foreach ($file_keys as $key) 
			{ $file_ary[$i][$key] = $file_post[$key][$i]; }
		}
		return $file_ary;
	}
}
?>